package com.bawei.ivideo

import android.annotation.SuppressLint
import android.app.*
import android.content.Intent
import android.os.Build
import android.text.TextUtils
import android.widget.RemoteViews
import com.alibaba.android.arouter.launcher.ARouter

import com.bawei.library_common.BaseApplication
import com.bawei.library_common.ConstanstValue
import com.bawei.library_common.notification.NotificationUtils
import com.bawei.library_common.utils.GsonUtil
import com.bawei.library_common.utils.LoggerUtils
import com.bawei.library_common.utils.UserEntity
import com.bawei.library_common.utils.UserUtils
import com.bawei.library_common.websocket.WebSocketUtils
import com.bawei.module_live.LiveActivity
import com.google.gson.Gson
import org.java_websocket.handshake.ServerHandshake
import java.lang.Exception

/**
 * @Author : yaotianxue
 * @Time : On 2023/6/19 11:29
 * @Description : App
 */
class App: BaseApplication() {
    override fun onCreate() {
        super.onCreate()
        initArouter()
        initWebSoccket()

    }
    //负责接受消息
    private fun initWebSoccket() {
        getWebSocket("",object :WebSocketUtils.ReceiveMsgListener{
            override fun onReceive(message: String?) {
                LoggerUtils.d("webSocket","接受到消息发送通知"+message)
                if(!TextUtils.isEmpty(message)){
                    if(message?.startsWith(ConstanstValue.LIVE_FLAG) == true){
                        // #live#|当前用户json串|直播间的名称(当前用户名称+随机数)
                        val split = message.split(ConstanstValue.SPLITFLAG)
                        //主播信息
                        val userEntity = Gson().fromJson<UserEntity>(split[1],UserEntity::class.java)
                        //直播间名称
                        val roomName = split[2]

                        val notificationUtils = NotificationUtils(this@App,0, R.drawable.ic_launcher_background, R.layout.no)
                        notificationUtils.setText(R.id.tv,"${userEntity.username}开始直播了！！！ 欢迎来围观")

                        val intent = Intent(this@App, LiveActivity::class.java)
                        intent.putExtra(ConstanstValue.AUTHORFLAG,userEntity.username)
                        intent.putExtra(ConstanstValue.ROOMNAMEFLAG,roomName)
                        notificationUtils.notified(intent)//带有页面跳转的发送通知


                    }
                }







            }

            override fun onOpen(handshakedata: ServerHandshake?) {

            }

            override fun onError(ex: Exception?) {

            }

            override fun onClose(code: Int, reason: String?, remote: Boolean) {

            }
        })
    }

    private fun initArouter() {
        //ARouter初始化
        ARouter.openDebug();
        ARouter.openLog();
        ARouter.init(this);//注意！！！init放在后面
    }


//    //发送通知：安卓8.0以后使用通道发送通知
//    @SuppressLint("RemoteViewLayout")
//    private fun sendNotification(message: String?) {
//        //0：通知管理
//        val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
//        //1：判断手机版本
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //8。0
//            val mChannel = NotificationChannel("1111", "通道的名称", NotificationManager.IMPORTANCE_DEFAULT)
//            mChannel.description = "通道的描述"
//            notificationManager.createNotificationChannel(mChannel)
//        }
//
//        //2：通知对象
//        //自定义view并为通知设置内容
//        val remoteViews = RemoteViews(packageName,R.layout.no)
//        remoteViews.setTextViewText(R.id.tv,message)
//        //点击通知跳转到其他页面
//        var pendingIntent: PendingIntent? = null
//        pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { //11.0
//            PendingIntent.getActivity(
//                this,
//                101,
//                Intent(this, LiveActivity::class.java),
//                PendingIntent.FLAG_IMMUTABLE
//            )
//        } else {
//            PendingIntent.getActivity(
//                this,
//                101,
//                Intent(this, LiveActivity::class.java),
//                PendingIntent.FLAG_UPDATE_CURRENT
//            )
//        }
//        var notification: Notification? = null
//        notification = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //8。0
//            Notification.Builder(this, "1111")
//                .setSmallIcon(R.mipmap.ic_launcher)
//                .setContentTitle("我是标题")
//                .setContentText("我是内容")
//                .setContentIntent(pendingIntent) //点击跳转
//                .setContent(remoteViews) //设置自定义布局
//                .build()
//        } else {
//            Notification.Builder(this)
//                .setSmallIcon(R.mipmap.ic_launcher)
//                .setContentTitle("我是标题")
//                .setContentText("我是内容")
//                .setContentIntent(pendingIntent) //点击跳转
//                .setContent(remoteViews) //设置自定义布局
//                .build()
//        }
//        //3：发送通知
//        notificationManager.notify(1, notification)
//    }

}